package com.aijk.ehm.sys.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.sys.mapper.BasRegionMapper;
import com.aijk.ehm.sys.model.BasRegion;
import com.aijk.ehm.sys.model.BasRegionExample;
import com.aijk.ehm.sys.model.qo.BasRegionQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BasRegionService {

	private static Logger logger = LoggerFactory.getLogger(BasRegionService.class);

	@Autowired
	private BasRegionMapper basRegionMapper;

	private BasRegionExample getExample(BasRegionQo qo){
		BasRegionExample example = new BasRegionExample();
		BasRegionExample.Criteria criteria = example.createCriteria();

		if (qo != null){
			// if (Utils.isNotEmpty(qo.getOrderbyclause())) example.setOrderByClause(qo.getOrderbyclause());
			if (Utils.isNotEmpty(qo.getId())) criteria.andIdEqualTo(qo.getId());
			if (Utils.isNotEmpty(qo.getTitle())) criteria.andTitleEqualTo(qo.getTitle());
			if (Utils.isNotEmpty(qo.getLevel())) criteria.andLevelEqualTo(qo.getLevel());
			if (Utils.isNotEmpty(qo.getLevels())) criteria.andLevelIn(qo.getLevels());
			if (Utils.isNotEmpty(qo.getLongcode())) criteria.andLongcodeEqualTo(qo.getLongcode());
			if (Utils.isNotEmpty(qo.getFlag())) criteria.andFlagEqualTo(qo.getFlag());
			if (Utils.isNotEmpty(qo.getPid())) criteria.andPidEqualTo(qo.getPid());
			if (Utils.isNotEmpty(qo.getIdIn())) criteria.andIdIn(qo.getIdIn());
			if (null!=qo.getCitylist() && qo.getCitylist().size() > 0) criteria.andIdIn(qo.getCitylist());
			if (Utils.isNotEmpty(qo.getFilerCity())) criteria.andTitleNotEqualTo(qo.getFilerCity());
		}
		return example;
	}

	public Integer count(BasRegionQo qo){
		return basRegionMapper.countByExample(getExample(qo));
	}

	public List<BasRegion> findAll() {
		return findAll(null);
	}

	public List<BasRegion> findAll(BasRegionQo qo) {
		return basRegionMapper.selectByExample(getExample(qo));
	}

	public List<BasRegion> findAll(BasRegionQo qo, Integer pageNumber, Integer pageSize) {
		PageHelper.startPage(pageNumber, pageSize);
		return basRegionMapper.selectByExample(getExample(qo));
	}

	public PageInfo<BasRegion> pageList(PagingRequest pagingRequest, BasRegionQo qo) {
		PageHelper.startPage(pagingRequest.getPageNum(), pagingRequest.getPageSize());
		return new PageInfo<BasRegion>(basRegionMapper.selectByExample(getExample(qo)));
	}

	public BasRegion findOne(Integer id) {
		return basRegionMapper.selectByPrimaryKey(id);
	}

	public Integer save(BasRegion record){
		return basRegionMapper.insertSelective(record);
	}

	public Integer update(BasRegion record) {
		return basRegionMapper.updateByPrimaryKeySelective(record);
	}

	public Integer delete(Integer id){
		return basRegionMapper.deleteByPrimaryKey(id);
	}
}
